package com.woniuxy.orderassembly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.common.PageResult;
import com.woniuxy.common.Result;
import com.woniuxy.form.ModifyComplainForm;
import com.woniuxy.form.OrderComplainForm;
import com.woniuxy.orderassembly.model.Order;
import com.woniuxy.orderassembly.model.OrderComplain;
import com.woniuxy.orderassembly.dao.OrderComplainMapper;
import com.woniuxy.orderassembly.service.IOrderComplainService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chestnut
 * @since 2021-05-24
 */
@Service
@Slf4j
public class OrderComplainServiceImpl extends ServiceImpl<OrderComplainMapper, OrderComplain> implements IOrderComplainService {

    @Resource
    private OrderComplainMapper orderComplainMapper;

    /**
     * 创建订单投诉表
     * @param orderComplainForm
     * @return
     */
    @Override
    public Result addOrderComplain(OrderComplainForm orderComplainForm) {
        OrderComplain orderComplain = new OrderComplain();
        log.info("生成订单投诉表信息,{}",orderComplainForm);
        BeanUtils.copyProperties(orderComplainForm,orderComplain);
        //生成订单投诉表时 默认为 1/待处理 2/已处理
        orderComplain.setStatus(1);
        orderComplain.setResult("嘀嘀嘀 默认未处理");
        int i = orderComplainMapper.insert(orderComplain);
        return i>0?Result.success("success"):Result.success("fail");
    }

    /**
     * 用户修改订单投诉表 根据id
     * @param modifyComplainForm
     * @return
     */
    @Override
    public Result modifyOrderComplain(ModifyComplainForm modifyComplainForm) {
        UpdateWrapper<OrderComplain> wrapper = new UpdateWrapper<>();
        log.info("修改订单投诉表信息,{}",modifyComplainForm);
        wrapper.set("reason",modifyComplainForm.getReason());
        wrapper.set("desc",modifyComplainForm.getDesc());
        wrapper.eq("id",modifyComplainForm.getId());
        int i = orderComplainMapper.update(null, wrapper);
        return i>0?Result.success("success"):Result.success("fail");
    }

    /**
     * 根据id删除订单投诉表
     * @param id
     * @return
     */
    @Override
    public Result delOrderComplain(Integer id) {
        log.info("根据id进行订单投诉表的删除,{}",id);
        int i = orderComplainMapper.deleteById(id);
        return i>0?Result.success("success"):Result.success("fail");
    }

    /**
     * 分页展示订单投诉表
     * @param pageIdx
     * @return
     */
    @Override
    public Result pageOrderComplain(Integer pageIdx) {
        //查询条件
        QueryWrapper<OrderComplain> wrapper = new QueryWrapper<>();
        //当前页码，每页条数
        Page<OrderComplain> pageParam = new Page(pageIdx,pageIdx);
        IPage<OrderComplain> pageResult = orderComplainMapper.selectPage(pageParam,wrapper);

        PageResult<OrderComplain> myPageResult =new PageResult<OrderComplain>((int)pageResult.getTotal(),(int)pageResult.getCurrent(),(int)pageResult.getSize(),pageResult.getRecords());
        myPageResult.setSuccess(true);
        return myPageResult;
    }

    @Override
    public Result pageOrderComplainByStatus(Integer status, Integer pageIdx) {
        //查询条件
        QueryWrapper<OrderComplain> wrapper = new QueryWrapper<>();
        wrapper.eq("status",status);
        //当前页码，每页条数
        Page<OrderComplain> pageParam = new Page(pageIdx,pageIdx);
        IPage<OrderComplain> pageResult = orderComplainMapper.selectPage(pageParam,wrapper);

        PageResult<OrderComplain> myPageResult =new PageResult<OrderComplain>((int)pageResult.getTotal(),(int)pageResult.getCurrent(),(int)pageResult.getSize(),pageResult.getRecords());
        myPageResult.setSuccess(true);
        return myPageResult;
    }
}
